#include "StdAfx.h"
#include "MyProtocol.h"

CMyProtocol::CMyProtocol(void)
{
}

CMyProtocol::~CMyProtocol(void)
{
}

void CMyProtocol::Login_read(IN char *streambuf, OUT char *name, OUT char *pwd)
{
	CByteStream bs((BYTE*)streambuf);
	int len = 0;
	bs>>len;
	bs.Read(name, len);
	bs>>len;
	bs.Read(pwd, len);
}

void CMyProtocol::Login_write(OUT char *streambuf, OUT int &length, IN char *name, IN char *pwd)
{
	CMPHeader mph = {0};
	mph._type = NPT_LOGIN;
	
	CMPBodyLogin mpbl = {0};
	mpbl.nlen = strlen(name);
	mpbl.plen = strlen(pwd);
	mph._bodylength = sizeof(int)*2 + mpbl.nlen + mpbl.plen;

	CByteStream bs((BYTE*)streambuf);
	bs<<mph._bodylength;
	bs<<mph._type;
	bs<<mpbl.nlen;
	bs.Write(name, mpbl.nlen);
	bs<<mpbl.plen;
	bs.Write(pwd, mpbl.plen);

	length = bs.Tell();
}

void CMyProtocol::Logout_write(OUT char *streambuf, OUT int &length, int uid)
{
	CMPHeader mph = {0};
	mph._type = NPT_LOGOUT;
	mph._bodylength = sizeof(uid);

	CByteStream bs((BYTE*)streambuf);
	bs<<mph._bodylength;
	bs<<mph._type;
	bs<<uid;

	length = bs.Tell();
}


void CMyProtocol::Register_write(OUT char *streambuf, OUT int &length, IN char *name, IN char *pwd)
{
	CMPHeader mph = {0};
	mph._type = NPT_REGISTRY;

	CMPBodyLogin mpbl = {0};
	mpbl.nlen = strlen(name);
	mpbl.plen = strlen(pwd);
	mph._bodylength = sizeof(int)*2 + mpbl.nlen + mpbl.plen;

	CByteStream bs((BYTE*)streambuf);
	bs<<mph._bodylength;
	bs<<mph._type;
	bs<<mpbl.nlen;
	bs.Write(name, mpbl.nlen);
	bs<<mpbl.plen;
	bs.Write(pwd, mpbl.plen);

	length = bs.Tell();
}

void CMyProtocol::Term_write(OUT char *streambuf, OUT int &length) 
{
	CMPHeader mph = {0};
	mph._type = NPT_TERM_SERVER;

	CMPBodyTerm mpbt;
	mpbt.cmd = 88;
	mph._bodylength = sizeof(mpbt.cmd);

	CByteStream bs((BYTE*)streambuf);
	bs<<mph._bodylength;
	bs<<mph._type;
	bs<<mpbt.cmd;

	length = bs.Tell();
}

// bodylength|type|blen|type|toid|fromid|msglen|msg
void CMyProtocol::Message_write(OUT char *streambuf, OUT int &length, int toUser, int fromUser, IN char *lpszMsg)
{
	CMPHeader mph = {0};
	mph._type = NPT_MESSAGE;

	CMPBodyMessage mpbm;
	mpbm.fromid = fromUser;
	mpbm.toid = toUser;
	mpbm.msglen = strlen(lpszMsg);

	int blen = sizeof(mpbm.fromid)+sizeof(mpbm.toid)+mpbm.msglen+sizeof(mpbm.msglen);
	mph._bodylength = blen + sizeof(mph._type) + sizeof(blen);


	CByteStream bs((BYTE*)streambuf);
	bs<<mph._bodylength;
	bs<<mph._type;

	bs<<blen;
	bs<<mph._type;
	bs<<mpbm.toid;
	bs<<mpbm.fromid;
	bs<<mpbm.msglen;
	bs.Write(lpszMsg, mpbm.msglen);

	length = bs.Tell();
}

/*
void CMyProtocol::Message_read(OUT char *streambuf, OUT int &length, int toUser, int fromUser, IN char *lpszMsg)
{
	// int length;
	// read(fd, length, sizeof(length);
	// unsigned char type;
	// read(fd, type, sizeof(type);
	CMPBodyMessage mpbm = {0};
	CByteStream bs((BYTE*)streambuf);
	bs>>mpbm.toid;
	bs>>mpbm.fromid;
	bs>>mpbm.msglen;
	mpbm.msg = new char[mpbm.msglen];
	memset(mpbm.msg, 0, mpbm.msglen);
	bs.Read(mpbm.msg, mpbm.msglen);
}
*/
